/**
 * (非组合)促销活动
 * @author miaoyl
 * @date 2018/01/28
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_param = {},
        select_data = [{code: '', value: '全部'}].push(app.datas_('crm_gwcd_gwcd005')),
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'GWDC001', width: 160, title: '活动编号', fixed: 'left',sort: true,search: 'default',searchOpts: {icon: true}}
            , {field: 'GWDCACTI', width: 80, title: '是否审核',
                filterDatas: app.datas_('crm_gwcd_gwcdacti'),fixed: 'left',search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'GWDC003', width: 160, title: '活动说明'}
            , {field: 'GWDC005', width: 160, title: '促销方式',sort: true,
                filterDatas: app.datas_('crm_gwcd_gwcd005').concat([{code: '', value: '全部'}]),
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'GWDC007', width: 160, title: '对象类型',sort: true,
                filterDatas: app.datas_('crm_gwcd_gwcd007').concat([{code: '', value: '全部'}]),
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'GWDC006', width: 160, title: '结算方式',sort: true,
                filterDatas: app.datas_('crm_gwcd_gwcd006').concat([{code: '', value: '全部'}]),
                search: 'default',
                searchOpts: {type: 'select', default: ''}
            }
            , {field: 'GWDC008', width: 160, title: '申请日期',sort: true}
            , {field: 'GWDC009', width: 160, title: '生效日期',sort: true}
            , {field: 'GWDC010', width: 160, title: '失效日期',sort: true}
            , {field: 'GWDC011_NAME', width: 160, title: '申请人员'}
            , {field: 'GWDC012_NAME', width: 160, title: '申请部门'}
            , {field: 'GWDC013_NAME', width: 160, title: '审核人'}
            , {field: 'GWDC015', width: 160, title: '备注'},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwdc003setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwdc003SettingAction,
        moduleName = '非组合促销活动'
    ;

    var Gwdc003 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwdc003',
                _searchs: [{
                    id: 'gwdc003', tableId: 'gwdc003_table', datas: _searchsArr.pushArray([
                        {field: 'GWDC016_1', width: 160, title: '渠道', search: 'default', searchOpts: {icon: true}},
                        {field: 'GWDC016_2', width: 160, title: '经销商', search: 'default', searchOpts: {icon: true}},
                        {field: 'GWDC018', width: 160, title: '产品编号', search: 'default', searchOpts: {icon: true}}
                    ])
                }],
                _loadTplsFull: [{
                    dom: 'gwdc003_edit', tpl: 'gwdc003_edit'
                },{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                },{
                    dom: 'gwdc003_edit_gwdc010', tpl: 'gwdc003_edit_gwdc010'
                }]
            }, win);
        },

        initTable: function () {
            /*var sort = {"field":"GWDC008","sort":"desc"};
            table_param['_SORT'] = JSON.stringify(sort);*/
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwdc003_table'),
                    url: g_ctxPath + 'crm/gwdc003/getGwdclist',
                    exportsUrl: g_ctxPath + 'crm/gwdc003/getGwdclist',
                    exportsMbUrl: g_ctxPath + 'files/促销价格批量导入.xls',
                    exportsName: moduleName+'数据',
                    title: moduleName+'信息表',
                    id: 'gwdc003_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_param
                });

                layui.laytpl(app.getByid(doc, 'script-gwdc003setting-action').innerHTML).render({}, function (string) {
                    gwdc003SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwdc003_table)', function (obj) {

                    layer.tips(gwdc003SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if (obj.data.GWDCACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的促销活动单才可以编辑！");
                                            return;
                                        }
                                        self._openTplFull({
                                            tag: 'gwdc003_edit',
                                            moduleName:moduleName,
                                            tagData:{obj:obj},
                                            index: 0,
                                            openOpts:{
                                                title: moduleName+'信息编辑',
                                                maxmin:true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if (obj.data.GWDCACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的促销活动单才可以删除！");
                                            return;
                                        }
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwdc003_table)', function (obj) {
                    var isCheck = true;
                    /*if (obj.data.GWDCACTI != 'N') {
                        isCheck = true;
                    }*/
                    self._openTplFull({
                        tag: 'gwdc003_edit',
                        moduleName:moduleName,
                        tagData: {
                            obj:obj,
                            isCheck:isCheck
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName+'信息编辑',
                            maxmin:true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwdc003_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwdc003_edit',
                                moduleName:moduleName,
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName+'信息编辑',
                                    maxmin:true
                                }
                            });
                            break;
                        case 'del':
                            var checkStatus = table.checkStatus('gwdc003_table');
                            var flag = true;
                            if (checkStatus.data.length > 0) {
                                var _xdata = [];
                                checkStatus.data.forEach(function (v) {
                                    var _temp = app.fields_('crm_gwdc');
                                    if (v.GWDCACTI != 'N') {
                                        app.showWarning("只有状态为N：未审核的促销活动单才可以删除！");
                                        flag = false;
                                        return;
                                    } else {
                                        _temp.GWDCENT = v.GWDCENT;
                                        _temp.GWDC001 = v.GWDC001;
                                        _xdata.push(_temp);
                                    }
                                });
                                if(flag) {
                                    app.confirm({
                                        msg: '是否删除选择的数据？',
                                        ok: function () {
                                            self.delDataBatch(_xdata);
                                        }
                                    });
                                }
                            }

                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'renew':
                            var checkStatus = table.checkStatus('gwdc003_table');
                            if (checkStatus.data.length > 1) {
                                app.showWarning("只允许单笔操作！");
                                return;
                            }
                            if (checkStatus.data.length < 1) {
                                app.showWarning("请先选择操作的单据！");
                                return;
                            }
                            if (checkStatus.data.length > 0) {
                                if (checkStatus.data[0].GWDCACTI != 'Y') {
                                    app.showWarning("只有状态为Y：已审核的促销活动单才可以修改！");
                                    return;
                                }else {
                                    self._openTplFull({
                                        tag: 'gwdc003_edit_gwdc010',
                                        moduleName:moduleName,
                                        tagData: {
                                            obj: checkStatus
                                        },
                                        index: 2,
                                        openOpts: {
                                            title: moduleName+'信息编辑',
                                            maxmin:true
                                        }
                                    });
                                }
                            }
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwdc003_table', where: table_param});
        },

        delData: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'crm/gwdc003/del',
                        xdata: obj.data,
                        loading: true,
                        okmsg: moduleName+'删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            });

        },

        delDataBatch: function(_xdata){
            app.ajax({
                xurl: 'crm/gwdc003/delDataBatch',
                xdata: _xdata,
                loading: true,
                toJsonStr: true,
                okmsg: moduleName+'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc003_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwdc');
                    if (v.GWDCACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的促销活动单才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWDCENT = v.GWDCENT;
                        _temp.GWDC001 = v.GWDC001;
                        _temp.GWDCACTI = "Y";
                        _xdata.push(_temp);
                    }
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwdc003/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg: '(非组合)促销活动审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        unCheck: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc003_table');
            if (checkStatus.data.length > 0) {
                var myDate = new Date();
                var year = myDate.getFullYear();
                var month = myDate.getMonth() + 1;
                var strDate = myDate.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                var time = year + '-' + month + '-' + strDate;
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('crm_gwdc');
                    if (v.GWDCACTI != 'Y') {
                        app.showWarning("只有状态为Y：已审核的促销活动单才可以取消审核！");
                        flag = false;
                        return;
                    }else {
                        _temp.GWDCENT = v.GWDCENT;
                        _temp.GWDC001 = v.GWDC001;
                        _temp.GWDCACTI = "N";
                        _xdata.push(_temp);
                    }
                    /*else if(!(v.GWDC009 > time)){
                        app.showWarning("只有生效日期大于当前时间才可以取消审核！");
                        flag = false;
                        return;
                    } */
                });
            } else {
                app.showWarning("请先选择单据！");
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否取消审核选择的数据？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwdc003/updateData',
                            xdata: _xdata,
                            loading: true,
                            toJsonStr: true,
                            okmsg:moduleName+ '取消审核成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwdc003',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWDC016_1') {//渠道开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwae001',
                                            columnList: [{label: '销售渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDC016_1 = data.GWAE001
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择渠道信息',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDC016_2') {//渠道商开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'cq_gwak001_4',
                                            columnList: [{label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDC016_2 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '470px']
                                    }
                                });
                            } else if (field == 'GWDC018') {//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwam001',
                                            columnList: [
                                                {label: '产品编号', prop: 'GWAM001', width: '150'},
                                                {label: '品名', prop: 'GWAML003', width: '150'},
                                                {label: '规格', prop: 'GWAML004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDC018 = data.GWAM001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '470px']
                                    }
                                });
                            }else if (field == 'GWDC001') {//活动开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            columnList: [{label: '活动编号', prop: 'GWDC001', width: '150'},
                                                {label: '活动说明', prop: 'GWDC003', width: '150'},
                                                {label: '日期', prop: 'GWDC008', width: '150'},
                                                {label: '状态码', prop: 'GWDCACTI_NAME', width: '150'}
                                            ],
                                            url: 'crm/gwdc003/getGwdc001Data'
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDC001 = data.GWDC001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择活动编号',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwdc003();

});